package study.service.impl;

import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import study.mapper.BrandMapper;
import study.pojo.Brand;
import study.pojo.PageBean;
import study.service.BrandService;
import study.util.SqlSessionFatoryUtils;

import java.util.List;

public class BrandServiceImpl implements BrandService {

    SqlSessionFactory factory = SqlSessionFatoryUtils.getSqlSessionFactory();

    /*
    * 查询所有
    * */
    public List<Brand>  selectAll(){
        //调用BrandMapper.selectAll()

        //2.获得SqlSession
        SqlSession sqlSession = factory.openSession();

        //3.获得BrandMapper
        BrandMapper mapper = sqlSession.getMapper(BrandMapper.class);

        //4.调用方法
        List <Brand> brands=mapper.selectAll();

        sqlSession.close();

        return brands;
    }

    /*
    *添加
    * */
    public void add(Brand brand){
        //2.获得SqlSession
        SqlSession sqlSession = factory.openSession();

        //3.获得BrandMapper
        BrandMapper mapper = sqlSession.getMapper(BrandMapper.class);

        //4.调用方法
        mapper.add(brand);

        //5.提交事务
        sqlSession.commit();

        sqlSession.close();
    }


    /*
    * 根据ID查询
    * */
    public Brand  selectById(int id){
        //调用BrandMapper.selectAll()

        //2.获得SqlSession
        SqlSession sqlSession = factory.openSession();

        //3.获得BrandMapper
        BrandMapper mapper = sqlSession.getMapper(BrandMapper.class);

        //4.调用方法
        Brand brand=mapper.selectById(id);

        sqlSession.close();

        return brand;
    }

    /*
     *修改
     * */
    public void update(Brand brand){
        //2.获得SqlSession
        SqlSession sqlSession = factory.openSession();

        //3.获得BrandMapper
        BrandMapper mapper = sqlSession.getMapper(BrandMapper.class);

        //4.调用方法
        mapper.update(brand);

        //5.提交事务
        sqlSession.commit();

        sqlSession.close();

    }

    /*
     *删除
     * */
    public void deleteById(int id){
        //2.获得SqlSession
        SqlSession sqlSession = factory.openSession();

        //3.获得BrandMapper
        BrandMapper mapper = sqlSession.getMapper(BrandMapper.class);

        //4.调用方法
        mapper.deleteById(id);

        //5.提交事务
        sqlSession.commit();

        sqlSession.close();

    }

    @Override
    public void deleteByIds(int[] ids) {
        //2.获得SqlSession
        SqlSession sqlSession = factory.openSession();

        //3.获得BrandMapper
        BrandMapper mapper = sqlSession.getMapper(BrandMapper.class);

        //4.调用方法
        mapper.deleteByIds(ids);

        //5.提交事务
        sqlSession.commit();

        sqlSession.close();
    }

    @Override
    public PageBean<Brand> selectByPage(int currentPage, int pageSize) {
        //2.获得SqlSession
        SqlSession sqlSession = factory.openSession();

        //3.获得BrandMapper
        BrandMapper mapper = sqlSession.getMapper(BrandMapper.class);

        /*
        * 计算开始索引
        * */
        int begin=(currentPage-1)*pageSize;
        /*
        * 计算查询条数
        * */
        int size=pageSize;

        //5.查询当页数据
        List<Brand> rows=mapper.selectByPage(begin,size);
                
        //6.查询数据总数
        int totalCount = mapper.selectTotalCount();

        //7.封装pageBean对象
        PageBean<Brand> pageBean=new PageBean<>();
        pageBean.setRows(rows);
        pageBean.setTotalCount(totalCount);

        //8.释放资源
        sqlSession.close();

        return pageBean;
    }

    @Override
    public PageBean<Brand> selectByPageAndCondition(int currentPage, int pageSize, Brand brand) {
        //2.获得SqlSession
        SqlSession sqlSession = factory.openSession();

        //3.获得BrandMapper
        BrandMapper mapper = sqlSession.getMapper(BrandMapper.class);

        /*
         * 计算开始索引
         * */
        int begin=(currentPage-1)*pageSize;
        /*
         * 计算查询条数
         * */
        int size=pageSize;
        
        /*
        * 处理brand条件，模糊表达式
        * */
        String brandName = brand.getBrandName();
        String companyName = brand.getCompanyName();

        if(brandName!=null && brandName.length()>0){
            brand.setBrandName("%"+brandName+"%");
        }
        if( companyName!=null &&  companyName.length()>0){
            brand.setCompanyName("%"+ companyName+"%");
        }

        //5.查询当页数据
        List<Brand> rows=mapper.selectByPageAndCondition(begin,size,brand);
        System.out.println(rows);
        //6.查询数据总数
        int totalCount = mapper.selectTotalCountByCondition(brand);
        System.out.println(totalCount);

        //7.封装pageBean对象
        PageBean<Brand> pageBean=new PageBean<>();
        pageBean.setRows(rows);
        pageBean.setTotalCount(totalCount);


        //8.释放资源
        sqlSession.close();

        return pageBean;
    }
}
